#include <iostream>
#include <unistd.h>
#include <signal.h>
using namespace std;

void handler(int signo)
{
    cout << "get a signal:" << signo << endl;
}
void PrintPending(sigset_t &pending)
{
    for (int i = 31; i >= 1; i--)
    {
        if (sigismember(&pending, i))
        {
            cout << "1";
        }
        else
        {
            cout << "0";
        }
    }
    cout << "\n\n";
}

int main()
{
    signal(2, handler);
    sigset_t bsit, osit;
    sigemptyset(&bsit);
    sigemptyset(&osit);
    sigaddset(&bsit, 2);
    sigprocmask(SIG_SETMASK, &bsit, &osit);

    sigset_t pending;
    sigemptyset(&pending);
    int cnt = 0;
    while (true)
    {
        int n = sigpending(&pending);
        if (n < 0)
            continue;
        PrintPending(pending);
        sleep(1);
        cnt++;
        if (cnt == 10)
        {
            cout << "解除" << endl;
            sigprocmask(SIG_SETMASK, &osit, nullptr);
        }
    }
    return 0;
}